<html>
<head><meta charset="utf-8"><title>Allocating custom DSTs · general · Zulip Chat Archive</title></head>
<h2>Stream: <a href="https://rust-lang.github.io/zulip_archive/stream/122651-general/index.html">general</a></h2>
<h3>Topic: <a href="https://rust-lang.github.io/zulip_archive/stream/122651-general/topic/Allocating.20custom.20DSTs.html">Allocating custom DSTs</a></h3>

<hr>

<base href="https://rust-lang.zulipchat.com">

<head><link href="https://rust-lang.github.io/zulip_archive/style.css" rel="stylesheet"></head>

<a name="224214244"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/122651-general/topic/Allocating%20custom%20DSTs/near/224214244" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Mario Carneiro <a href="https://rust-lang.github.io/zulip_archive/stream/122651-general/topic/Allocating.20custom.20DSTs.html#224214244">(Jan 27 2021 at 17:14)</a>:</h4>
<p>What's the current recommended way of allocating DSTs like:</p>
<div class="codehilite" data-code-language="Rust"><pre><span></span><code><span class="k">struct</span> <span class="nc">Foo</span><span class="w"> </span><span class="p">{</span><span class="w"></span>
<span class="w">  </span><span class="n">a</span>: <span class="kt">bool</span><span class="p">,</span><span class="w"></span>
<span class="w">  </span><span class="n">b</span>: <span class="kt">u64</span><span class="p">,</span><span class="w"></span>
<span class="w">  </span><span class="n">c</span>: <span class="p">[</span><span class="kt">u32</span><span class="p">]</span><span class="w"></span>
<span class="p">}</span><span class="w"></span>

<span class="k">fn</span> <span class="nf">make_foo</span><span class="p">(</span><span class="n">a</span>: <span class="kt">bool</span><span class="p">,</span><span class="w"> </span><span class="n">b</span>: <span class="kt">u64</span><span class="p">,</span><span class="w"> </span><span class="n">n</span>: <span class="kt">usize</span><span class="p">)</span><span class="w"> </span>-&gt; <span class="nb">Box</span><span class="o">&lt;</span><span class="n">Foo</span><span class="o">&gt;</span><span class="w"> </span><span class="p">{</span><span class="w"></span>
<span class="w">  </span><span class="nb">Box</span>::<span class="n">new</span><span class="p">(</span><span class="n">Foo</span><span class="w"> </span><span class="p">{</span><span class="n">a</span><span class="p">,</span><span class="w"> </span><span class="n">b</span><span class="p">,</span><span class="w"> </span><span class="n">c</span>: <span class="p">[</span><span class="mi">0</span><span class="p">;</span><span class="w"> </span><span class="n">n</span><span class="p">]})</span><span class="w"></span>
<span class="p">}</span><span class="w"></span>
</code></pre></div>
<p>Last I checked, there was basically no way to do this without unsafe code, and even with unsafe code you had to guess at offsets and so on. Has this changed at all since custom DSTs were introduced? I'm porting some C code that uses these kinds of trailing arrays and I'm not sure what the most robust Rust analogue is.</p>



<a name="224214436"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/122651-general/topic/Allocating%20custom%20DSTs/near/224214436" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> bjorn3 <a href="https://rust-lang.github.io/zulip_archive/stream/122651-general/topic/Allocating.20custom.20DSTs.html#224214436">(Jan 27 2021 at 17:16)</a>:</h4>
<p>We don't have custom DSTs yet.</p>



<a name="224214438"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/122651-general/topic/Allocating%20custom%20DSTs/near/224214438" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Jonas Schievink  [he/him] <a href="https://rust-lang.github.io/zulip_archive/stream/122651-general/topic/Allocating.20custom.20DSTs.html#224214438">(Jan 27 2021 at 17:16)</a>:</h4>
<p>You can to it safely via coercion if you have <code>struct Foo&lt;T: ?Sized&gt;</code> instead of a fixed <code>[u32]</code> field</p>



<a name="224214519"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/122651-general/topic/Allocating%20custom%20DSTs/near/224214519" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> bjorn3 <a href="https://rust-lang.github.io/zulip_archive/stream/122651-general/topic/Allocating.20custom.20DSTs.html#224214519">(Jan 27 2021 at 17:16)</a>:</h4>
<p>That coercion requires a certain unstable trait to be implemented. (<code>CoerceUnsized</code>?)</p>



<a name="224214530"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/122651-general/topic/Allocating%20custom%20DSTs/near/224214530" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Mario Carneiro <a href="https://rust-lang.github.io/zulip_archive/stream/122651-general/topic/Allocating.20custom.20DSTs.html#224214530">(Jan 27 2021 at 17:16)</a>:</h4>
<p><span class="user-mention" data-user-id="211727">@Jonas Schievink</span> That only works if <code>n</code> is a constant, right?</p>



<a name="224214549"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/122651-general/topic/Allocating%20custom%20DSTs/near/224214549" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Mario Carneiro <a href="https://rust-lang.github.io/zulip_archive/stream/122651-general/topic/Allocating.20custom.20DSTs.html#224214549">(Jan 27 2021 at 17:16)</a>:</h4>
<p>or at least is a constant in all uses</p>



<a name="224214612"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/122651-general/topic/Allocating%20custom%20DSTs/near/224214612" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Jonas Schievink  [he/him] <a href="https://rust-lang.github.io/zulip_archive/stream/122651-general/topic/Allocating.20custom.20DSTs.html#224214612">(Jan 27 2021 at 17:17)</a>:</h4>
<p>I think so</p>



<a name="224214670"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/122651-general/topic/Allocating%20custom%20DSTs/near/224214670" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Mario Carneiro <a href="https://rust-lang.github.io/zulip_archive/stream/122651-general/topic/Allocating.20custom.20DSTs.html#224214670">(Jan 27 2021 at 17:17)</a>:</h4>
<p>I don't mind using unsafe code here, I just want to not use footgunny code</p>



<a name="224214786"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/122651-general/topic/Allocating%20custom%20DSTs/near/224214786" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Mario Carneiro <a href="https://rust-lang.github.io/zulip_archive/stream/122651-general/topic/Allocating.20custom.20DSTs.html#224214786">(Jan 27 2021 at 17:18)</a>:</h4>
<p>I suspect there is a way to use <code>Layout</code> to achieve this, not sure if the relevant tools are stable though</p>



<a name="224215285"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/122651-general/topic/Allocating%20custom%20DSTs/near/224215285" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Mario Carneiro <a href="https://rust-lang.github.io/zulip_archive/stream/122651-general/topic/Allocating.20custom.20DSTs.html#224215285">(Jan 27 2021 at 17:22)</a>:</h4>
<p>In particular, how do I set up all the other fields, <code>a</code> and <code>b</code> here? I guess I need a layout for the header, then <code>layout.pad_to_align()?.extend(Layout::array::&lt;T&gt;(n)?)?</code> should do the trick, but I'm not sure how to get the header layout safely</p>



<a name="224215375"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/122651-general/topic/Allocating%20custom%20DSTs/near/224215375" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Jonas Schievink  [he/him] <a href="https://rust-lang.github.io/zulip_archive/stream/122651-general/topic/Allocating.20custom.20DSTs.html#224215375">(Jan 27 2021 at 17:23)</a>:</h4>
<p>Unless you use <code>#[repr]</code>, you cannot rely on the precise field layout</p>



<a name="224215403"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/122651-general/topic/Allocating%20custom%20DSTs/near/224215403" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Mario Carneiro <a href="https://rust-lang.github.io/zulip_archive/stream/122651-general/topic/Allocating.20custom.20DSTs.html#224215403">(Jan 27 2021 at 17:23)</a>:</h4>
<p>I thought there would be a chapter in the nomicon or something for this but docs seem to be scant</p>



<a name="224215626"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/122651-general/topic/Allocating%20custom%20DSTs/near/224215626" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Mario Carneiro <a href="https://rust-lang.github.io/zulip_archive/stream/122651-general/topic/Allocating.20custom.20DSTs.html#224215626">(Jan 27 2021 at 17:24)</a>:</h4>
<p>Right <span class="user-mention" data-user-id="211727">@Jonas Schievink</span> , that's the footgunny part. I'm hoping that <code>Layout</code> will let me do this even for <code>repr(Rust)</code></p>



<a name="224216105"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/122651-general/topic/Allocating%20custom%20DSTs/near/224216105" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Mario Carneiro <a href="https://rust-lang.github.io/zulip_archive/stream/122651-general/topic/Allocating.20custom.20DSTs.html#224216105">(Jan 27 2021 at 17:27)</a>:</h4>
<p>Is it really that unusable? I thought there was <em>some</em> point in adding the syntax</p>



<a name="224246466"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/122651-general/topic/Allocating%20custom%20DSTs/near/224246466" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Quy Nguyen <a href="https://rust-lang.github.io/zulip_archive/stream/122651-general/topic/Allocating.20custom.20DSTs.html#224246466">(Jan 27 2021 at 21:00)</a>:</h4>
<p>DSTs will always be the last field, so there's some math you can do. Iirc if you like at some custom smart pointer crates you'll probably see it.</p>



<a name="224382790"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/122651-general/topic/Allocating%20custom%20DSTs/near/224382790" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Daniel Henry-Mantilla <a href="https://rust-lang.github.io/zulip_archive/stream/122651-general/topic/Allocating.20custom.20DSTs.html#224382790">(Jan 28 2021 at 19:48)</a>:</h4>
<p><span class="user-mention" data-user-id="271719">@Mario Carneiro</span> I wrote a lengthy post about it in the forums, showcasing both the pattern to support coercions and thus stack allocations, and how to do it with a manual heap allocation: <a href="https://users.rust-lang.org/t/rust-wrapper-around-c-flexible-array/52605/2?u=yandros">https://users.rust-lang.org/t/rust-wrapper-around-c-flexible-array/52605/2?u=yandros</a></p>



<a name="224545539"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/122651-general/topic/Allocating%20custom%20DSTs/near/224545539" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> The 8472 <a href="https://rust-lang.github.io/zulip_archive/stream/122651-general/topic/Allocating.20custom.20DSTs.html#224545539">(Jan 29 2021 at 22:38)</a>:</h4>
<blockquote>
<p>I'm hoping that Layout will let me do this even for repr(Rust)</p>
</blockquote>
<p>Probably not since each generic instantiation could in principle have a different order for the other fields, even if the dynamic part always comes last.</p>



<a name="224904119"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/122651-general/topic/Allocating%20custom%20DSTs/near/224904119" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Josh Triplett <a href="https://rust-lang.github.io/zulip_archive/stream/122651-general/topic/Allocating.20custom.20DSTs.html#224904119">(Feb 02 2021 at 17:17)</a>:</h4>
<p><span class="user-mention" data-user-id="303115">@Quy Nguyen</span> In theory, if we have native support for custom DSTs, they don't <em>have</em> to be the last field, as long as you don't mind it being more expensive to get at later fields.</p>



<a name="224904210"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/122651-general/topic/Allocating%20custom%20DSTs/near/224904210" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Josh Triplett <a href="https://rust-lang.github.io/zulip_archive/stream/122651-general/topic/Allocating.20custom.20DSTs.html#224904210">(Feb 02 2021 at 17:17)</a>:</h4>
<p>And as long as you can compute the length of each field using only the fields before it.</p>



<a name="224905646"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/122651-general/topic/Allocating%20custom%20DSTs/near/224905646" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> bjorn3 <a href="https://rust-lang.github.io/zulip_archive/stream/122651-general/topic/Allocating.20custom.20DSTs.html#224905646">(Feb 02 2021 at 17:26)</a>:</h4>
<p>That would make <code>offset_of!</code> impossible for such type.</p>



<hr><p>Last updated: Aug 07 2021 at 22:04 UTC</p>
</html>